ggplot (continued)

Installing colorblindr

install.packages("remotes")
library(remotes)
remotes::install_github("clauswilke/colorblindr") 
# update all of the recommended packages when prompted
# if an error is generated, re-start RStudio and re-run this line of code

Preliminaries

library(ggplot2)
library(ggthemes)
library(patchwork)
library(colorblindr)
library(cowplot)
library(colorspace)
library(wesanderson)
library(ggsci)
library(TeachingDemos)

char2seed("Dark Star")
d <- mpg

Bar plots

# use to plot the counts of rows for a categorical variable
table(d$drv)
## 
##   4   f   r 
## 103 106  25
p1 <- ggplot(d,aes(x=drv)) + geom_bar(color="black",fill="cornsilk")
print(p1)

# aesthetic mapping gives multiple groups for each bar
p1 <- ggplot(d,aes(x=drv,fill=fl)) + geom_bar()
print(p1)

# stacked, but need to adjust color transparency, which is "alpha"
p1 <- ggplot(d,aes(x=drv,fill=fl)) + geom_bar(alpha = 0.3, position="identity")
print(p1)

# better to use position = fill for stacking, but with equivalent height
p1 <- ggplot(d,aes(x=drv,fill=fl)) + geom_bar(position="fill")
print(p1)

# best to use position = dodge for multiple bars
p1 <- ggplot(d,aes(x=drv,fill=fl)) + geom_bar(position="dodge",color="black",size=1)
print(p1)

# more typical "bar plot" has heights as the values themselves
dTiny <- tapply(X=d$hwy,INDEX=as.factor(d$fl),FUN=mean) #calculate the means
dTiny <- data.frame(hwy=dTiny) # create a single-column data frame
dTiny <- cbind(fl=row.names(dTiny),dTiny) # 

p2 <- ggplot(dTiny, aes(x=fl,y=hwy,fill=fl)) +
  geom_col()
print(p2)

#### Use a box plot instead of standard "means" bars!
# basic boxplot is simple and informative
p1 <- ggplot(d,aes(x=fl,y=hwy,fill=fl)) +
               geom_boxplot()
print(p1)

# now overlay the raw data
p1 <- ggplot(d,aes(x=fl,y=hwy)) +
               geom_boxplot(fill="thistle",outlier.shape=NA) + 
  # geom_point()
  geom_point(position=position_jitter(width=0.1,height=0.7),color="grey60",size=2)

print(p1)

### Using Colors

  1. Aesthetics
  1. Colors that are attractive - large geoms (fills) - pale colors - small geoms(lines,points) - bright colors
  2. Colors that highlight elements - pale, grey to de-emphasize - bright or saturated colors to emphasize
  3. Colors that are visible to the color blind
  4. Colors that convert well to black and white
  1. Information content
  1. Discrete scale
  • colors to group similar treatments
  • neutral colors (black,grey,white) to indicate control groups
  • Symbolic colors (heat=red, cool = blue, photosynthesis/growth=green, oligotrophic=blue, eutrophic=brown, infected=red)
  • Colors that map to chemical stains or gels, or even colors of organisms
  1. Continuous scale
  • monochromatic (differing shades of 1 color)
  • 2 tone chromatic scale (from color x to color y)
  • 3 tone divergent scale (from color x through color y to color z)
  1. Use color information within and between graphs
  1. show color names, hex in base R
  2. show color schemes in colorbrewer

Color visualizations

my_cols <- c("thistle","tomato","cornsilk","cyan","chocolate")
demoplot(my_cols,"map")

demoplot(my_cols,"bar")

demoplot(my_cols,"scatter")

demoplot(my_cols,"heatmap")

demoplot(my_cols,"spine")

demoplot(my_cols,"perspective")

Working with black and white

# gray function versus gray colors

# built in greys (0 = black, 100 = white
my_greys <- c("grey20","grey50","grey80")
demoplot(my_greys,"bar")

my_greys2 <- grey(seq(from=0.1,to=0.9,length.out=10))         
demoplot(my_greys2,"heatmap")

# converting color plots to black and white
p1 <- ggplot(d,aes(x=as.factor(cyl),y=cty,fill=as.factor(cyl))) + geom_boxplot() 
plot(p1)

# default colors look identical in black white
p1_des<- colorblindr::edit_colors(p1, desaturate)
plot(p1_des)

# custom colors not pretty, but convert ok to bw
p2 <- p1 + scale_fill_manual(values=c("red","blue","green","yellow"))
plot(p2)

p2_des<- colorblindr::edit_colors(p2, desaturate)
plot(p2_des)

using alpha transparency for histograms

x1 <- rnorm(n=100,mean=0)
x2 <- rnorm(n=100,mean=2.7)
dFrame <- data.frame(v1=c(x1,x2))
lab <- rep(c("Control","Treatment"),each=100)
dFrame <- cbind(dFrame,lab)
str(dFrame)
## 'data.frame':    200 obs. of  2 variables:
##  $ v1 : num  -0.0306 1.4022 0.166 2.5987 -1.1781 ...
##  $ lab: chr  "Control" "Control" "Control" "Control" ...
h1 <- ggplot(dFrame,aes(x=v1,fill=lab))
h1 + geom_histogram(position="identity",alpha=0.5,color="black") 

Color customizations

d <- mpg
# --------- discrete classification
# scale_fill_manual for boxplots,bars
# scale_color_manual for points, lines

# boxplot no color
p_fil <- ggplot(d,aes(x=as.factor(cyl),y=cty))
p_fil + geom_boxplot()

# boxplot default ggplot fill
p_fil <- ggplot(d,aes(x=as.factor(cyl),y=cty,fill=as.factor(cyl))) + geom_boxplot()
plot(p_fil)

# create custom color palette
my_cols <- c("red","brown","blue","orange")


# boxplot with custom colors for fill
p_fil + scale_fill_manual(values=my_cols)

# scatterplot with no color
p_col <- ggplot(d,aes(x=displ,y=cty))
p_col + geom_point(size=3)

# scatterplot default ggplot colors
p_col <- ggplot(d,aes(x=displ,y=cty,col=as.factor(cyl))) + geom_point(size=3)
plot(p_col)

# scatterplot with custom colors for point color
p_col + scale_color_manual(values=my_cols)

# ------- continuous classification (color gradient)

# default color gradient
p_grad <- ggplot(d,aes(x=displ,y=cty,col=hwy)) + geom_point(size=3)
plot(p_grad)

# custom sequential gradient (2-colors)
p_grad + scale_color_gradient(low="green", high="red")

# custom diverging gradient (3-colors)
mid <- median(d$cty)
p_grad + scale_color_gradient2(midpoint=mid,
                               low="blue",
                               mid="white",
                               high="red")

# custom diverging gradient (n-colors
p_grad + scale_color_gradientn(colors=c("blue","green","yellow","purple","orange"))

Tour of color palettes

Wes Anderson palettes
library(wesanderson)
print(wes_palettes)
## $BottleRocket1
## [1] "#A42820" "#5F5647" "#9B110E" "#3F5151"
## [5] "#4E2A1E" "#550307" "#0C1707"
## 
## $BottleRocket2
## [1] "#FAD510" "#CB2314" "#273046" "#354823"
## [5] "#1E1E1E"
## 
## $Rushmore1
## [1] "#E1BD6D" "#EABE94" "#0B775E" "#35274A"
## [5] "#F2300F"
## 
## $Rushmore
## [1] "#E1BD6D" "#EABE94" "#0B775E" "#35274A"
## [5] "#F2300F"
## 
## $Royal1
## [1] "#899DA4" "#C93312" "#FAEFD1" "#DC863B"
## 
## $Royal2
## [1] "#9A8822" "#F5CDB4" "#F8AFA8" "#FDDDA0"
## [5] "#74A089"
## 
## $Zissou1
## [1] "#3B9AB2" "#78B7C5" "#EBCC2A" "#E1AF00"
## [5] "#F21A00"
## 
## $Darjeeling1
## [1] "#FF0000" "#00A08A" "#F2AD00" "#F98400"
## [5] "#5BBCD6"
## 
## $Darjeeling2
## [1] "#ECCBAE" "#046C9A" "#D69C4E" "#ABDDDE"
## [5] "#000000"
## 
## $Chevalier1
## [1] "#446455" "#FDD262" "#D3DDDC" "#C7B19C"
## 
## $FantasticFox1
## [1] "#DD8D29" "#E2D200" "#46ACC8" "#E58601"
## [5] "#B40F20"
## 
## $Moonrise1
## [1] "#F3DF6C" "#CEAB07" "#D5D5D3" "#24281A"
## 
## $Moonrise2
## [1] "#798E87" "#C27D38" "#CCC591" "#29211F"
## 
## $Moonrise3
## [1] "#85D4E3" "#F4B5BD" "#9C964A" "#CDC08C"
## [5] "#FAD77B"
## 
## $Cavalcanti1
## [1] "#D8B70A" "#02401B" "#A2A475" "#81A88D"
## [5] "#972D15"
## 
## $GrandBudapest1
## [1] "#F1BB7B" "#FD6467" "#5B1A18" "#D67236"
## 
## $GrandBudapest2
## [1] "#E6A0C4" "#C6CDF7" "#D8A499" "#7294D4"
## 
## $IsleofDogs1
## [1] "#9986A5" "#79402E" "#CCBA72" "#0F0D0E"
## [5] "#D9D0D3" "#8D8680"
## 
## $IsleofDogs2
## [1] "#EAD3BF" "#AA9486" "#B6854D" "#39312F"
## [5] "#1C1718"
demoplot(wes_palettes$BottleRocket1,"pie")

demoplot(wes_palettes[[2]][1:3],"bar")

my_cols <- wes_palettes$GrandBudapest2[1:4]
p_fil + scale_fill_manual(values=my_cols)

Color Brewer palettes
library(RColorBrewer)
display.brewer.all()

display.brewer.all(colorblindFriendly=TRUE)

demoplot(brewer.pal(4,"Accent"),"bar")

demoplot(brewer.pal(11,"Spectral"),"heatmap")

my_cols <- c("grey75",brewer.pal(3,"Blues"))
p_fil + scale_fill_manual(values=my_cols)

# nice for seeing hex values!
library(scales)
show_col(my_cols)

Viridis palettes
#### Making a heat map
xVar <- 1:30
yVar <- 1:5
myData <- expand.grid(xVar=xVar,yVar=yVar)
head(myData)
##   xVar yVar
## 1    1    1
## 2    2    1
## 3    3    1
## 4    4    1
## 5    5    1
## 6    6    1
zVar <- myData$xVar + myData$yVar + 2*rnorm(n=150)
myData <- cbind(myData,zVar)
head(myData)
##   xVar yVar     zVar
## 1    1    1 1.930717
## 2    2    1 4.783829
## 3    3    1 4.301021
## 4    4    1 3.626993
## 5    5    1 6.506057
## 6    6    1 5.774694
# default gradient colors in ggplot
p4 <- ggplot(myData,aes(x=xVar,y=yVar,fill=zVar)) +
  geom_tile()
print(p4)

# user defined divergent palette
p4 + scale_fill_gradient2(midpoint=19,low="brown",mid=grey(0.8),high="darkblue")

# viridis scale
p4  + scale_fill_viridis_c()

# options viridis, cividis, magma, inferno, plasma
p4 + scale_fill_viridis_c(option="inferno")

#desaturated viridis
p4 <- p4 + geom_tile() + scale_fill_viridis_c() 
p4des<-edit_colors(p4, desaturate)
ggdraw(p4des)